package com.bwsoft.portal.action;

import java.io.File;
import java.io.PrintWriter;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.sf.json.JSONObject;

import org.apache.log4j.Logger;

import com.bwsoft.admin.domain.Account;
import com.bwsoft.api.pojo.ResponsePojo;
import com.bwsoft.util.ValidatorUtil;

public class AccountAction extends PortalBaseAction {

	private static Logger LOG = Logger.getLogger(AccountAction.class);
	
	private int accountType;
	private Integer accountId;
	private String compName;
	private String compNatureName;
	private String linkMan;
	private String phone;
	private String address;
	private String nickName;
	private Integer sex;
	private Date birthday;
	private String email;
	private String fax;
	private Integer compNature;
	private String password;
	
	private File headImgURL;
	private String headImgURLFileName; // 文件名称
	
	Account account;
	
	List<HashMap> natureStr;
	
	public String showAccount() {
		accountId = ((Account)session.get("Account")).getAccountId();
		account = accountService.findAccount(accountId);
		//通过${account.nickName}在前台获取时会缓存，获取不到数据库更新后的数据
		nickName = account.getNickName();
		linkMan = account.getLinkMan();
		phone = account.getPhone();
		address = account.getAddress();
		sex = account.getSex() ? 1 : 0;
		birthday = account.getBirthday();
		linkMan = account.getLinkMan();
		email = account.getEmail();
		fax = account.getFax();
		compNature = account.getCompNature();
		headImgURLFileName = account.getHeadImgURL();
		accountType = account.getAccountType();
		password = account.getPassword();
		//检验人员用户通过compid获取公司名，其他用户直接获取compName
		if (account.getAccountType() == 3) {
			Account comp = account.getCompany();
			if (comp != null) {
				compName = account.getCompany().getCompName();
			}
		} else {
			compName = account.getCompName();
		}
		
		compNatureName = dictionaryDataService.getDicValueDataByNameValue("comp_nature", compNature);
		
		if (account.getAccountType() != null && (account.getAccountType() == 1 || account.getAccountType() == 2))
			return "pcpersonalinfocompany";
		else
			return "pcpersonalinfo";
	}
	
	public String showEditAccount() {
		accountId = ((Account)session.get("Account")).getAccountId();
		account = accountService.findAccount(accountId);
		nickName = account.getNickName();
		linkMan = account.getLinkMan();
		phone = account.getPhone();
		address = account.getAddress();
		sex = account.getSex() ? 1 : 0;
		birthday = account.getBirthday();
		linkMan = account.getLinkMan();
		email = account.getEmail();
		fax = account.getFax();
		compNature = account.getCompNature();
		headImgURLFileName = account.getHeadImgURL();
		accountType = account.getAccountType();
		password = account.getPassword();
		if (account.getAccountType() == 3) {
			Account comp = account.getCompany();
			if (comp != null) {
				compName = account.getCompany().getCompName();
			}
		} else {
			compName = account.getCompName();
		}
		
		// 获取机构性质
		ResponsePojo responsePoj= dictionaryService.getData("comp_nature");
		List<Map<String, Object>> returnMap = (List<Map<String, Object>>)responsePoj.getData();
		natureStr = (List<HashMap>)returnMap.get(0).get("dicData");
		
		if (account.getAccountType() != null && (account.getAccountType() == 1 || account.getAccountType() == 2))
			return "pcpersonalinfocompanyedit";
		else
			return "pcpersonalinfoedit";
	}
	
	public String editPerson() {
		if (nickName == null || "".equals(nickName)) { 
			responseJson.put("result", 1);
			responseJson.put("msg", "用户名不能为空！");
			return "mapJsonResult";
		}
		if (email != null && !"".equals(email) && !ValidatorUtil.isEmail(email)) { 
			responseJson.put("result", 2);
			responseJson.put("msg", "邮箱格式不正确！");
			return "mapJsonResult";
		}
		String headImgURLType = "";
		if (headImgURLFileName != null && !"".equals(headImgURLFileName)) {
			headImgURLType = headImgURLFileName.substring(headImgURLFileName.lastIndexOf(".")).toLowerCase();
		}
		if(headImgURLFileName != null && !"".equals(headImgURLFileName) && !qualityControlService.isImage(headImgURLType)){
			responseJson.put("result", 3);
			responseJson.put("msg", "上传的头像不是图片类型！");
			return "mapJsonResult";
		}
		if(headImgURLFileName != null && !"".equals(headImgURLFileName) && !newsService.isFitSize(headImgURL)){
			responseJson.put("result", 4);
			responseJson.put("msg", "上传的头像不能超过8M！");
			return "mapJsonResult";
		}
		Account company = accountService.findAccountByCompName(compName);
		if (accountType == 3 && compName != null && !"".equals(compName) && company == null) {
			responseJson.put("result", 5);
			responseJson.put("msg", "单位名称不存在！");
			return "mapJsonResult";
		}
		Account account = new Account();
		account.setAccountId(accountId);
		account.setAccount(phone);
		account.setNickName(nickName);
		account.setEmail(email);
		account.setSex(sex == 1);
		account.setBirthday(birthday);
		account.setPhone(phone);
		account.setAccountType(accountType);
		account.setPassword(password);
		account.setAddress(address);
		if (accountType == 3) {
			account.setCompany(company);
		} else {
			account.setCompName(compName);
		}
		ResponsePojo rp = accountService.update(account,headImgURL,headImgURLFileName);
		//easyui返回的json的contentType必须为text/html
		response.setContentType("text/html;charset=utf-8");
		JSONObject json = new JSONObject();
		json.put("result", rp.getErrcode());
		json.put("msg", rp.getErrmsg());
		try {
			PrintWriter out = response.getWriter();
			out.write(json.toString());
			out.flush();
			out.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return null;
	}
	
	public String editCompany() {
		if (nickName == null || "".equals(nickName)) { 
			responseJson.put("result", 1);
			responseJson.put("msg", "用户名不能为空！");
			return "mapJsonResult";
		}
		if (compName == null || "".equals(compName)) { 
			responseJson.put("result", 2);
			responseJson.put("msg", "机构名称不能为空！");
			return "mapJsonResult";
		}
		if (linkMan == null || "".equals(linkMan)) { 
			responseJson.put("result", 3);
			responseJson.put("msg", "联系人不能为空！");
			return "mapJsonResult";
		}
		if (phone == null || "".equals(phone)) { 
			responseJson.put("result", 4);
			responseJson.put("msg", "联系电话不能为空！");
			return "mapJsonResult";
		}
		if (!ValidatorUtil.isMobile(phone)) { 
			responseJson.put("result", 5);
			responseJson.put("msg", "手机号格式不正确！");
			return "mapJsonResult";
		}
		if (address == null || "".equals(address)) { 
			responseJson.put("result", 6);
			responseJson.put("msg", "机构地址不能为空！");
			return "mapJsonResult";
		}
		if (email == null || "".equals(email)) { 
			responseJson.put("result", 7);
			responseJson.put("msg", "电子邮箱不能为空！");
			return "mapJsonResult";
		}
		if (!ValidatorUtil.isEmail(email)) { 
			responseJson.put("result", 8);
			responseJson.put("msg", "邮箱格式不正确！");
			return "mapJsonResult";
		}
		if (accountService.checkAccount(email, accountId)) {
			responseJson.put("result", 9);
			responseJson.put("msg", "此邮箱已经注册！");
			return "mapJsonResult";
		}
		String headImgURLType = "";
		if (headImgURLFileName != null && !"".equals(headImgURLFileName)) {
			headImgURLType = headImgURLFileName.substring(headImgURLFileName.lastIndexOf(".")).toLowerCase();
		}
		if(headImgURLFileName != null && !"".equals(headImgURLFileName) && !qualityControlService.isImage(headImgURLType)){
			responseJson.put("result", 10);
			responseJson.put("msg", "上传的头像不是图片类型！");
			return "mapJsonResult";
		}
		if(headImgURLFileName != null && !"".equals(headImgURLFileName) && !newsService.isFitSize(headImgURL)){
			responseJson.put("result", 11);
			responseJson.put("msg", "上传的头像不能超过8M！");
			return "mapJsonResult";
		}
		Account account = new Account();
		account.setAccountId(accountId);
		account.setAccount(email);
		account.setNickName(nickName);
		account.setEmail(email);
		account.setCompName(compName);
		account.setPhone(phone);
		account.setAccountType(accountType);
		account.setPassword(password);
		account.setAddress(address);
		account.setCompNature(compNature);
		account.setLinkMan(linkMan);
		account.setFax(fax);
		ResponsePojo rp = accountService.update(account,headImgURL,headImgURLFileName);
		//easyui返回的json的contentType必须为text/html
		response.setContentType("text/html;charset=utf-8");
		JSONObject json = new JSONObject();
		json.put("result", rp.getErrcode());
		json.put("msg", rp.getErrmsg());
		try {
			PrintWriter out = response.getWriter();
			out.write(json.toString());
			out.flush();
			out.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return null;
	}

	public Account getAccount() {
		return account;
	}

	public void setAccount(Account account) {
		this.account = account;
	}

	public int getAccountType() {
		return accountType;
	}

	public void setAccountType(int accountType) {
		this.accountType = accountType;
	}

	public Integer getAccountId() {
		return accountId;
	}

	public void setAccountId(Integer accountId) {
		this.accountId = accountId;
	}

	public String getCompName() {
		return compName;
	}

	public void setCompName(String compName) {
		this.compName = compName;
	}

	public String getLinkMan() {
		return linkMan;
	}

	public void setLinkMan(String linkMan) {
		this.linkMan = linkMan;
	}

	public String getPhone() {
		return phone;
	}

	public void setPhone(String phone) {
		this.phone = phone;
	}

	public String getAddress() {
		return address;
	}

	public void setAddress(String address) {
		this.address = address;
	}

	public String getNickName() {
		return nickName;
	}

	public void setNickName(String nickName) {
		this.nickName = nickName;
	}

	public Integer getSex() {
		return sex;
	}

	public void setSex(Integer sex) {
		this.sex = sex;
	}

	public Date getBirthday() {
		return birthday;
	}

	public void setBirthday(Date birthday) {
		this.birthday = birthday;
	}

	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	public String getFax() {
		return fax;
	}

	public void setFax(String fax) {
		this.fax = fax;
	}

	public Integer getCompNature() {
		return compNature;
	}

	public void setCompNature(Integer compNature) {
		this.compNature = compNature;
	}

	public File getHeadImgURL() {
		return headImgURL;
	}

	public void setHeadImgURL(File headImgURL) {
		this.headImgURL = headImgURL;
	}

	public String getHeadImgURLFileName() {
		return headImgURLFileName;
	}

	public void setHeadImgURLFileName(String headImgURLFileName) {
		this.headImgURLFileName = headImgURLFileName;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public List<HashMap> getNatureStr() {
		return natureStr;
	}

	public void setNatureStr(List<HashMap> natureStr) {
		this.natureStr = natureStr;
	}

	public String getCompNatureName() {
		return compNatureName;
	}

	public void setCompNatureName(String compNatureName) {
		this.compNatureName = compNatureName;
	}
}
